﻿class Solution {
public:
    int calculate(string s) {
        vector<int> st; // ⽤数组来模拟栈结构
        int i = 0, n = s.size();
        char op = '+';
        while (i < n)
        {
            if (s[i] == ' ') i++;
            else if (s[i] >= '0' && s[i] <= '9')
            {
                // 先把这个数字给提取出来
                int tmp = 0;
                while (i < n && s[i] >= '0' && s[i] <= '9')
                    tmp = tmp * 10 + (s[i++] - '0');
                if (op == '+') st.push_back(tmp);
                else if (op == '-') st.push_back(-tmp);
                else if (op == '*') st.back() *= tmp;
                else st.back() /= tmp;
            }
            else
            {
                op = s[i];
                i++;
            }
        }
        int ret = 0;
        for (auto x : st) ret += x;
        return ret;
    }
};